package com.ozy.kxreader.app

import com.ozy.kxreader.base.IView
import com.ozy.kxreader.app.Lifecycleable
import com.trello.rxlifecycle2.LifecycleTransformer
import com.trello.rxlifecycle2.android.RxLifecycleAndroid

/**
 * Created by 欧源 on 2018/2/1.
 */
class RxLifecycleUtils {


    init {
        throw IllegalStateException("you can't instantiate me!")
    }


    companion object {


        fun <T> bindToLifecycle(view: IView?): LifecycleTransformer<T> {
            return if (view is Lifecycleable<*>) {
                bindToLifecycle(view as Lifecycleable<*>)
            } else {
                throw IllegalArgumentException("view isn't Lifecycleable")
            }
        }

        fun <T> bindToLifecycle(lifecycleable: Lifecycleable<*>?): LifecycleTransformer<T> {
            return if (lifecycleable is ActivityLifecycleable) {
                RxLifecycleAndroid.bindActivity(lifecycleable.provideLifecycleSubject())
            } else if (lifecycleable is FragmentLifecycleable) {
                RxLifecycleAndroid.bindFragment(lifecycleable.provideLifecycleSubject())
            } else {
                throw IllegalArgumentException("Lifecycleable not match")
            }
        }
    }
}